*! 程振兴 2018年3月21日
*! cnmaproute 北京 上海 广州 阜阳 郑州 徐州 南京 荆州
cap prog drop cnmaproute
prog define cnmaproute
	version 14.0
	syntax anything [, HTMLname(string)]
	if "`htmlname'" == "" local htmlname = "fw_example"
	cap preserve
	qui clear
	tokenize `anything'
	local num: word count `anything'
	qui{
		set obs `num'
		gen city = ""
		forval i = 1/`=_N'{
			replace city = "``i''" in `i'
		}
		chinagcode, baidukey(tXvh2UTmo4z0zq5t41XLPTm1kQbMHjYl) city(city)
		jscopy
		fw, s h(`htmlname')
		lib, b
		fw `"var geoCoordMap = {"'
		forval i = 1/`=_N'{
			local var = `" '`=city[`i']'': [`=longitude[`i']', `=latitude[`i']'], "'
			fw `"`var'"'
		}
		fw `"};"'
		fw `"var fromdata = '';"'
		fw `"var BJData = ["'
	    forval i = 1/`num'{
	    	local var = `"[{name: '``i'''}, {name: '``i'''}],"'
	    	fw `"`var'"'
	    }
	    local num1 = `num' - 1
	    forval i = 1/`num1'{
	    	local j = `i' + 1
	    	local var = `"[{name: '``i'''}, {name: '``j'''}],"'
	    	fw `"`var'"'
	    }
		fw `"    ];"'
		fw `"var convertData = function (data) {"'
		fw `"    var res = [];"'
		fw `"    for (var i = 0; i < data.length; i++) {"'
		fw `"        var dataItem = data[i];"'
		fw `"        var fromCoord = geoCoordMap[dataItem[0].name];"'
		fw `"        var toCoord = geoCoordMap[dataItem[1].name];"'
		fw `"        if (fromCoord && toCoord) {"'
		fw `"            res.push({"'
		fw `"                fromName: dataItem[0].name,"'
		fw `"                toName: dataItem[1].name,"'
		fw `"                coords: [fromCoord, toCoord]"'
		fw `"            });"'
		fw `"        }"'
		fw `"    }"'
		fw `"    return res;"'
		fw `"};"'
		fw `"var color = ['#f6d23b'];"'
		fw `"var series = [];"'
		fw `"[[fromdata, BJData]].forEach(function (item, i) {"'
		fw `"    series.push({"'
		fw `"        name: item[0],"'
		fw `"        type: 'lines',"'
		fw `"        zlevel: 1,"'
		fw `"        effect: {"'
		fw `"            show: true,"'
		fw `"            period: 6,"'
		fw `"            trailLength: 0.7,"'
		fw `"            color: '#fff',"'
		fw `"            symbolSize: 3"'
		fw `"        },"'
		fw `"        lineStyle: {"'
		fw `"            normal: {"'
		fw `"                color: color[i],"'
		fw `"                width: 0,"'
		fw `"                curveness: 0.2//维度"'
		fw `"            }"'
		fw `"        },"'
		fw `"        data: convertData(item[1])"'
		fw `"    },"'
		fw `"    {"'
		fw `"        name: item[0],"'
		fw `"        type: 'lines',"'
		fw `"        zlevel: 2,"'
		fw `"        symbol: ['none', 'none'],"'
		fw `"        symbolSize: 10,"'
		fw `"        effect: {"'
		fw `"            show: true,"'
		fw `"            period: 6,"'
		fw `"            trailLength: 0,"'
		fw `"            symbol: 'none',"'
		fw `"            symbolSize: 8"'
		fw `"        },"'
		fw `"        label: {"'
		fw `"                  normal: {"'
		fw `"                      show: true,"'
		fw `"                      position: 'right',"'
		fw `"                      formatter: '{b}'"'
		fw `"                  }"'
		fw `"              },"'
		fw `"        lineStyle: {"'
		fw `"            normal: {"'
		fw `"                color: color[i],"'
		fw `"                width: 1,"'
		fw `"                opacity: 0.6,"'
		fw `"                curveness:0.2//维度"'
		fw `"            }"'
		fw `"        },"'
		fw `"        data: convertData(item[1])"'
		fw `"    },"'
		fw `"    {"'
		fw `"        name: item[0],"'
		fw `"        type: 'effectScatter',"'
		fw `"        coordinateSystem: 'geo',"'
		fw `"        zlevel: 2,"'
		fw `"        rippleEffect: {"'
		fw `"            brushType: 'stroke'"'
		fw `"        },"'
		fw `"        label: {"'
		fw `"            normal: {"'
		fw `"                show: true,"'
		fw `"                position: 'right',"'
		fw `"                formatter: '{b}'"'
		fw `"            }"'
		fw `"        },"'
		fw `"        symbolSize:10,"'
		fw `"        itemStyle: {"'
		fw `"            normal: {"'
		fw `"                areaColor:'#fff',"'
		fw `"                color: color[i]"'
		fw `"            }"'
		fw `"        },"'
		fw `"        data: item[1].map(function (dataItem) {"'
		fw `"            return {"'
		fw `"                name: dataItem[1].name,"'
		fw `"                value: geoCoordMap[dataItem[1].name].concat([dataItem[1].value]),"'
		fw `"                state: geoCoordMap[dataItem[1].name].concat([dataItem[1].state]),"'
		fw `"                baojing: geoCoordMap[dataItem[1].name].concat([dataItem[1].baojing]),"'
		fw `"                guzhang: geoCoordMap[dataItem[1].name].concat([dataItem[1].guzhang]),"'
		fw `"                tips: geoCoordMap[dataItem[1].name].concat([dataItem[1].tips]),"'
		fw `"                shiyan: geoCoordMap[dataItem[1].name].concat([dataItem[1].shiyan]),"'
		fw `"                zhidong: geoCoordMap[dataItem[1].name].concat([dataItem[1].zhidong]),"'
		fw `"                zouhang: geoCoordMap[dataItem[1].name].concat([dataItem[1].zouhang]),"'
		fw `"                fanghuo: geoCoordMap[dataItem[1].name].concat([dataItem[1].fanghuo]),"'
		fw `"                video: geoCoordMap[dataItem[1].name].concat([dataItem[1].video]),"'
		fw `"                jueyuan: geoCoordMap[dataItem[1].name].concat([dataItem[1].jueyuan]),"'
		fw `"                jiankong: geoCoordMap[dataItem[1].name].concat([dataItem[1].jiankong]),"'
		fw `"                liegong: geoCoordMap[dataItem[1].name].concat([dataItem[1].liegong]),"'
		fw `"                weiji: geoCoordMap[dataItem[1].name].concat([dataItem[1].weiji]),"'
		fw `"            };"'
		fw `"        })"'
		fw `"    });"'
		fw `"});"'
		fw `"option = {"'
		fw `"    backgroundColor: '#031528',"'
		fw `"    tooltip : {"'
		fw `"        trigger: 'item',"'
		fw `"        enterable:true,"'
		fw `"    },"'
		fw `"    geo: {"'
		fw `"        map: 'china',"'
		fw `"        zoom:1.2,"'
		fw `"        label: {"'
		fw `"            emphasis: {"'
		fw `"                show: false"'
		fw `"            }"'
		fw `"        },"'
		fw `"        roam: true,"'
		fw `"        itemStyle: {"'
		fw `"            normal: {"'
		fw `"                areaColor: '#000002',"'
		fw `"                borderColor: '#404a59'"'
		fw `"            },"'
		fw `"            emphasis: {"'
		fw `"                areaColor: '#2a333d'"'
		fw `"            }"'
		fw `"        }"'
		fw `"    },"'
		fw `"    series: series,"'
		fw `"};"'
		fw, e t
	}
	if "`htmlname'" !="" di `"点击{browse `htmlname'.html:`htmlname'.html}即可打开结果文件"'
	if "`htmlname'" =="" di `"点击{browse fw_example.html:fw_example.html}即可打开结果文件"'
	if "`c(os)'" == "MacOSX"{
		cap qui drm __MACOSX
	}
	if "`htmlname'" !="" copen `htmlname'.html
	if "`htmlname'" =="" copen fw_example.html
end 

